﻿#region Imports
using System.Reflection;
using CommonUtilities.Extensions;
using CommonUtilities.Properties;
using Microsoft.VisualBasic.ApplicationServices;
#endregion

namespace CommonUtilities.Info
{
    /// <summary>
    /// Application info
    /// </summary>
    public static class AppInfo
    {
        #region Properties
        /// <summary>
        /// Assembly of currently executing application
        /// </summary>
        private static Assembly CurrentAssembly
        {
            get { return Assembly.GetEntryAssembly(); }
        }

        /// <summary>
        /// Supporting property for AssemblyInfo
        /// </summary>
        private static AssemblyInfo assemblyInfo = CurrentAssembly.IsNull() ? null : new AssemblyInfo(CurrentAssembly);
        /// <summary>
        /// Assembly Info of currently executing application
        /// </summary>
        private static AssemblyInfo CurrentAssemblyInfo
        {
            get { return assemblyInfo; }
        }

        /// <summary>
        /// Assembly title
        /// </summary>
        public static string AssemblyTitle
        {
            get { return CurrentAssemblyInfo.Coalesce("Title", Resources.Const_NotAvailable); }
        }

        /// <summary>
        /// Product version
        /// </summary>
        public static string AssemblyVersion
        {
            get { return CurrentAssemblyInfo.Coalesce("Version", Resources.Const_NotAvailable); }
        }

        public static string AssemblyVersionShort
        {
            get
            {
                var version = CurrentAssemblyInfo.Version;
                string version_str = "{0}.{1}";

                if (version.Revision > 0) version_str += ".{2}.{3}";
                else if (version.Build > 0) version_str += ".{2}";

                return version_str.FormatTextFromParams(version.Major, version.Minor, version.Build, version.Revision)
                    .Coalesce(Resources.Const_NotAvailable);
            }
        }

        /// <summary>
        /// Product description
        /// </summary>
        public static string AssemblyDescription
        {
            get { return CurrentAssemblyInfo.Coalesce("Description", Resources.Const_NotAvailable); }
        }

        /// <summary>
        /// Product name
        /// </summary>
        public static string AssemblyProduct
        {
            get { return CurrentAssemblyInfo.Coalesce("ProductName", Resources.Const_NotAvailable); }
        }

        /// <summary>
        /// Product copyright info
        /// </summary>
        public static string AssemblyCopyright
        {
            get { return CurrentAssemblyInfo.Coalesce("Copyright", Resources.Const_NotAvailable); }
        }

        /// <summary>
        /// Product company
        /// </summary>
        public static string AssemblyCompany
        {
            get { return CurrentAssemblyInfo.Coalesce("CompanyName", Resources.Const_NotAvailable); }
        }

        /// <summary>
        /// Start-up path of the assembly
        /// </summary>
        public static string AssemblyLocation
        {
            get { return CurrentAssemblyInfo.IsNull() ? Resources.Const_NotAvailable : CurrentAssembly.Location; }
        }

        /// <summary>
        /// Application title
        /// </summary>
        public static string ApplicationTitle
        {
            get { return "{0} v{1} - {2}".FormatTextFromParams(AssemblyTitle, AssemblyVersionShort, AssemblyDescription); }
        }

        /// <summary>
        /// Email of author of product
        /// </summary>
        public static string AuthorEmail
        {
            get { return Resources.Const_AuthorEmail; }
        }
        #endregion
    }
}
